package com.djzhou.gmms.service.role.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.djzhou.gmms.model.entity.PageBean;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.gmms.service.base.impl.BaseServiceImpl;
import com.djzhou.gmms.service.role.RoleService;

public class RoleServiceImpl  extends BaseServiceImpl implements RoleService{

	@Override
	public void add(TbRole t) {
		roleDAO.insert(t);
	}

	@Override
	public void delete(Integer id) {
		TbRole t=roleDAO.queryByID(TbRole.class, id);
		t.setDeleteFlag(1);
		roleDAO.update(t);
	}

	@Override
	public TbRole queryById(Integer id) {
		
		return roleDAO.queryByID(TbRole.class, id);
	}

	@Override
	public List<TbRole> queryList(HashMap<String, Object> hashMap) {
		String hql="from TbRole role where role.deleteFlag=0 and role.roleId<>1";
		String companyId=null;
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("companyId")!=null){
				companyId=hashMap.get("companyId").toString();
				sb.append(" and role.company.companyId=").append(companyId);
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		return roleDAO.query(hql);
	}

	@Override
	public PageBean<TbRole> queryPageBean(HashMap<String, Object> hashMap,
			int pageSize, int currentPage) {
		String hql="from TbRole role where role.deleteFlag=0 and role.roleId<>1";
		String companyId=null,departmentId=null;
		StringBuffer sb=new StringBuffer();
		sb.append(hql);
		if(hashMap!=null && hashMap.size()!=0){
			if(hashMap.get("companyId")!=null && !hashMap.get("companyId").equals(1)){
				companyId=hashMap.get("companyId").toString();
				sb.append(" and (role.company.companyId=1 or role.company.companyId=").append(companyId).append(")");
			}
			if(hashMap.get("departmentId")!=null){
				departmentId=hashMap.get("departmentId").toString();
				sb.append(" and (role.department.departmentId=1 or role.department.departmentId=").append(departmentId).append(")");
			}
		}
		sb.append(" order by createTime desc");
		hql=sb.toString();
		return roleDAO.selectByPage(hql, pageSize, currentPage);
	}

	@Override
	public void update(TbRole t) {
		roleDAO.update(t);
	}

	@Override
	public List<TbRole> queryList(Integer companyId) {
		if(companyId!=1){
			String hql="from TbRole role where role.deleteFlag=0 and role.roleId<>1 and role.company.companyId in(1,?)";
			return roleDAO.query(hql,companyId);
		}else{
			String hql="from TbRole role where role.deleteFlag=0";
			return roleDAO.query(hql);
		}
		
	}
	
	@Override
	public VoResource query4AutoComplete(HashMap<String, Object> hashMap, String sidx,
			String sord, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TbRole> queryList(String roleIds) {
		String hql="from TbRole role where role.roleId in("+roleIds+")";
		return roleDAO.query(hql);
	}
	

}
